package com.graduation.check.controller;

import com.graduation.check.dto.DashboardStatsResponse;
import com.graduation.check.dto.RecentRecordResponse;
import com.graduation.check.service.PaperInfoExtractionStatsService;
import com.graduation.check.service.TitleCheckService;
import com.graduation.check.utils.ApiResponse;
import com.graduation.check.entity.User;
import com.graduation.check.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/student/dashboard")  // context-path=/api已在application.properties配置
public class DashboardStatsController {

    @Autowired
    private TitleCheckService titleCheckService;

    @Autowired
    private PaperInfoExtractionStatsService extractionStatsService;

    @Autowired
    private UserMapper userMapper;

    /**
     * 获取题目查重统计
     */
    @GetMapping("/title-check-stats")
    public ApiResponse<DashboardStatsResponse> getTitleCheckStats(Authentication authentication) {
        try {
            Long studentId = getCurrentStudentId(authentication);
            log.info("获取题目查重统计 - 学生ID: {}", studentId);

            DashboardStatsResponse stats = titleCheckService.getTitleCheckStats(studentId);
            return ApiResponse.success(stats);

        } catch (Exception e) {
            log.error("获取题目查重统计失败", e);
            return ApiResponse.error(500, "获取统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取信息提取统计
     */
    @GetMapping("/extraction-stats")
    public ApiResponse<DashboardStatsResponse> getExtractionStats(Authentication authentication) {
        try {
            Long studentId = getCurrentStudentId(authentication);
            log.info("获取信息提取统计 - 学生ID: {}", studentId);

            DashboardStatsResponse stats = extractionStatsService.getExtractionStats(studentId);
            return ApiResponse.success(stats);

        } catch (Exception e) {
            log.error("获取信息提取统计失败", e);
            return ApiResponse.error(500, "获取统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取最近查重记录
     */
    @GetMapping("/recent-records")
    public ApiResponse<List<RecentRecordResponse>> getRecentRecords(
            Authentication authentication,
            @RequestParam(defaultValue = "5") int limit) {
        try {
            Long studentId = getCurrentStudentId(authentication);
            log.info("获取最近查重记录 - 学生ID: {}, 限制: {}", studentId, limit);

            List<RecentRecordResponse> records = titleCheckService.getRecentRecords(studentId, limit);
            return ApiResponse.success(records);

        } catch (Exception e) {
            log.error("获取最近查重记录失败", e);
            return ApiResponse.error(500, "获取记录失败: " + e.getMessage());
        }
    }

    /**
     * 从认证信息中获取学生ID
     */
    private Long getCurrentStudentId(Authentication authentication) {
        if (authentication != null && authentication.isAuthenticated()) {
            String username = authentication.getName();
            log.debug("获取当前用户 - 用户名: {}", username);

            User user = userMapper.findByAccount(username);
            if (user == null) {
                throw new RuntimeException("用户不存在: " + username);
            }

            log.debug("用户信息获取成功 - 用户ID: {}, 角色: {}", user.getId(), user.getUserType());
            return user.getId();
        }
        throw new RuntimeException("用户未认证");
    }
}